﻿using Castle.MicroKernel.Registration;
using Castle.Windsor;
using DevelopmentKit.Extensions.SharpArch.DataContext;
using DevelopmentKit.Extensions.SharpArch.Repositorys;
using SharpArch.Domain.Commands;
using SharpArch.Domain.PersistenceSupport;
using SharpArch.NHibernate;
using SharpArch.NHibernate.Contracts.Repositories;

namespace DevelopmentKit.Web.CastleWindsor
{
    public class ComponentRegistrar
    {
        public static void AddComponentsTo(IWindsorContainer container) 
        {

            container.Register(
               Component.For(typeof(IEntityDuplicateChecker))
                   .ImplementedBy(typeof(EntityDuplicateChecker))
                   .Named("entityDuplicateChecker"));

            container.Register(
                Component.For(typeof(INHibernateRepository<>))
                    .ImplementedBy(typeof(NHibernateRepository<>))
                    .Named("nhibernateRepositoryType")
                    .Forward(typeof(IRepository<>)));

            container.Register(
                Component.For(typeof(INHibernateRepositoryWithTypedId<,>))
                    .ImplementedBy(typeof(NHibernateRepositoryWithTypedId<,>))
                    .Named("nhibernateRepositoryWithTypedId")
                    .Forward(typeof(IRepositoryWithTypedId<,>)));

            container.Register(
                    Component.For(typeof(ISessionFactoryKeyProvider))
                        .ImplementedBy(typeof(DefaultSessionFactoryKeyProvider))
                        .Named("sessionFactoryKeyProvider"));



            container.Register(
                Component.For(typeof(IENSRepositoryWithTypedId<,>))
                    .ImplementedBy(typeof(ENSRepositoryWithTypedId<,>))
                    .Named("ENSRepositoryWithTypedId")
                    .Forward(typeof(INHibernateRepositoryWithTypedId<,>)));


            container.Register(
                Component.For(typeof(IENSRepository<>))
                    .ImplementedBy(typeof(ENSRepository<>))
                    .Named("ENSRepository")
                    .Forward(typeof(INHibernateRepository<>)));

            container.Register(
                  Component.For(typeof(IDataContext))
                      .ImplementedBy(typeof(NHibernateDataContext))
                      .Named("nhibernateDataContext"));

            //container.Register(
            //        Component.For(typeof(ILogger))
            //            .ImplementedBy(typeof(FileLogger))
            //            .Named("fileLogger"));

            container.Register(
                  AllTypes
                      .FromAssemblyNamed("DevelopmentKit.Core")
                      .BasedOn(typeof(IRepositoryWithTypedId<,>))
                      .WithService.FirstInterface());
            container.Register(
                AllTypes.FromAssemblyNamed("DevelopmentKit.Web")
                    .BasedOn<NHibernateQuery>()
                    .WithService.DefaultInterfaces());

            container.Register(
                AllTypes.FromAssemblyNamed("DevelopmentKit.Core")
                    .BasedOn(typeof(NHibernateQuery))
                    .WithService.DefaultInterfaces()); 
        }
         

       

       
         
    }
}